import { _decorator, Component, find, Input, Node } from 'cc';
import { getAdaptiveRect } from 'db://assets/scripts/Utils';
import { SkillDialog } from './SkillDialog';
import { SkillItem } from './SkillItem';
import { GameManager } from 'db://assets/scripts/GameManager';
import { UiManager } from 'db://assets/scripts/UiManager';
import { HeroItem } from 'db://assets/scripts/HeroItem';
import { EnemyItem } from 'db://assets/scripts/EnemyItem';
import { Dialog } from 'db://assets/scripts/Dialog';
const { ccclass, property } = _decorator;

@ccclass('SkillDialogController')
export class SkillDialogController extends Component {
    currentSkillNode: Node;
    skillDialog: SkillDialog;
    targets: Node[] = [];
    cacheTargets: Node[] = [];

    init(): void {
        this.node.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
    }
    protected onDestroy(): void {
        this.node.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);
    }

    clear() {
        this.currentSkillNode = null;
        this.skillDialog.restoreSkillTargets(this.cacheTargets);
        this.targets = [];
    }

    protected onTouchStart(event): void {
        this.skillDialog.skillContentNode.children.forEach(skillNode => {
            if (getAdaptiveRect(skillNode).contains(event.getUILocation())) {
                this.currentSkillNode = skillNode;
                this.targets = this.cacheTargets = (this.currentSkillNode.getComponent(SkillItem) as SkillItem).data.originData
                    .targets({ system: (GameManager.instance as GameManager).palBattleSystem }).map(
                        target => (UiManager.instance as UiManager).getHeroNode(target)
                    );
                this.skillDialog.setSkillTargets(this.targets);
            }
        });
    }
    protected onTouchMove(event): void {
        if (!this.currentSkillNode) return;
        const skillComponent = (this.currentSkillNode.getComponent(SkillItem) as SkillItem);
        if (skillComponent.data.originData.needTarget) {
            this.targets = [];
            this.skillDialog.targetContentNode.children.forEach(targetNode => {
                if (getAdaptiveRect(targetNode).contains(event.getUILocation())) {
                    this.targets.push(targetNode);
                }
            });
        }
    }
    protected onTouchEnd(event): void {
        if (this.targets.length > 0) {
            (GameManager.instance as GameManager).useSkill(
                (this.currentSkillNode.getComponent(SkillItem) as SkillItem).data,
                this.targets.map(node => {
                    if (node.getComponent(HeroItem)) {
                        return node.getComponent(HeroItem).data.saveData.UUID;
                    }
                    if (node.getComponent(EnemyItem)) {
                        return node.getComponent(EnemyItem).data.saveData.UUID;
                    }
                })
            );
        }
        this.clear();
        this.skillDialog.getComponent(Dialog).close();
    }
}


